package heman.server.controllers;


import heman.server.entities.PlayerManager;
//import heman.server.entities.Table;
import heman.server.entities.Player;
import heman.server.entities.Table;
import heman.server.entities.TableManager;
import heman.server.entities.TableType;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.server.interfaces.IProcessServerMessage;

/**
 *  Join controller handles JOIN message.
 *	<xs:element name="join">
 *		<xs:complexType>
 *		<xs:attribute name="table" type="table-id" use="required" /> 
 *		</xs:complexType>
 *	</xs:element>
 *
 * @author Yanyi Zhang
 *
 */
public class JoinController implements IProcessServerMessage {
	public boolean process(Communicator com, Message m){
		String originator = m.getOriginator();
		NamedNodeMap map=m.contents().getAttributes();
		String tableID=map.
        		getNamedItem("table").getNodeValue();
		Table TargetTable=TableManager.getInstance().getTable(tableID);
		Player TargetPlayer=PlayerManager.getInstance().getPlayer(originator);
		Message r;
		if(TargetTable.getPlayerIDs().size()>3) return false;
		//table is full, ignore the message
		if(TargetTable.getType().equals(TableType.PUBLIC)){
			TargetTable.addPlayer(TargetPlayer);
			if(TargetTable.getPlayerIDs().size()==1)
				TargetTable.setModerator(TargetPlayer);
			StringBuilder sb = new StringBuilder(Message.responseHeader(true,m.id));
	        sb.append("<tableResponse>");
	        sb.append(TargetTable.toXML());
	        sb.append("</tableResponse></response>");
	        Document d = Message.construct(sb.toString());
	        r = new Message (d);
	        r.setOriginator(originator);
	        r.setRecipient(originator);//respond to originator
	        r.setBroadcast();//respond to others
	        return com.distribute(r);
		} else if(TargetTable.getType().equals(TableType.PRIVATE)){
			//This means at least one player at table
			StringBuilder sb = new StringBuilder(Message.requestHeader());
			sb.append("<confirm table=\"");
			sb.append(tableID);
			sb.append("\" player=\"");
			sb.append(originator);
			sb.append("\"/></request>");
			Document d = Message.construct(sb.toString());
		    r = new Message (d);
		    r.setOriginator(originator);
		    r.setRecipient(TargetTable.getModerator().getId());
		    //send to moderator
		    return com.distribute(r);
		} else if(TargetTable.getType().equals(TableType.BY_INVITATION)){
			if(TargetTable.removeinvitePlayer(TargetPlayer))
				TargetTable.addPlayer(TargetPlayer);
			StringBuilder sb = new StringBuilder(Message.responseHeader(true,m.id));
	        sb.append("<tableResponse>");
	        sb.append(TargetTable.toXML());
	        sb.append("</tableResponse></response>");
	        Document d = Message.construct(sb.toString());
	        r = new Message (d);
	        r.setOriginator(originator);
	        r.setRecipient(originator);//respond to originator
	        r.setBroadcast();//respond to others
	        return com.distribute(r);
		}
		return false;//ignore other situation
	}
}
